package com.viphuli.fragment;

import java.util.ArrayList;
import java.util.List;

import android.support.v4.app.Fragment;
import android.widget.BaseAdapter;
import android.widget.ListView;
import butterknife.Bind;

import com.loopj.android.http.RequestParams;
import com.viphuli.app.R;
import com.viphuli.http.ApiRequest;
import com.viphuli.http.bean.part.PurchasePayComment;
import com.viphuli.http.handler.MyBaseHttpResponseHandler.HandlerType;

public class HomeServiceInfoCommentFragment extends BaseProgressFragment {

	@Bind(R.id.id_main_home_service_item_info_comment_listview)
	protected ListView commentListView;
	
	private HomeServiceInfoFragment serviceInfoFrag;

	private List<PurchasePayComment> list = new ArrayList<PurchasePayComment>();
	private BaseAdapter adapter;

	@Override
	protected int obtainContentRes() {
		return R.layout.frag_home_service_comment;
	}

	@Override
	protected void initParams() {

	}

	@Override
	protected void initView() {
		setCanRefresh(false);
		
		List<Fragment> fs = getFragmentManager().getFragments();
		serviceInfoFrag = (HomeServiceInfoFragment) fs.iterator().next();
		
		//顶部显示&隐藏控制
				serviceInfoFrag = (HomeServiceInfoFragment) fs.iterator().next();
				serviceInfoFrag.setScrollTouchEvent(commentListView);
	}

	@Override
	protected void initData() {
		clean();
		initRequest(HandlerType.loading);
	}

	@Override
	protected void initRequest(HandlerType handlerType) {

		//下拉刷新操作恢复初始状态
		if (HandlerType.refresh.equals(handlerType)) {
			clean();
		}

		List<Fragment> fs = getFragmentManager().getFragments();
		HomeServiceInfoFragment serviceInfoFrag = (HomeServiceInfoFragment) fs.iterator().next();

		//网络请求
		RequestParams params = new RequestParams();
		params.put("service_item_id", serviceInfoFrag.getServiceItemId());
		
		if (HandlerType.more.equals(handlerType)) {
			params.put("page", page + 1);
		} else {
			params.put("page", page);
		}
		
		ApiRequest.homeRoomServiceComment.request(this, params, handlerType);
	}

	public ListView getListView() {
		return commentListView;
	}

	public List<PurchasePayComment> getList() {
		return list;
	}

	public void setBaseAdapter(BaseAdapter adapter) {
		this.adapter = adapter;
	}
	
	public BaseAdapter getBaseAdapter() {
		return adapter;
	}

	private void clean() {
		list.clear();
		page = 1;
	}

}
